package com.mxpio.erp.purchase.controller;

import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.purchase.entity.PurchaseOrderLine;
import com.mxpio.erp.common.purchase.service.PurchaseOrderLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "PurchaseOrderLineController", tags = { "采购订单行数据接口" })
@RestController
@RequestMapping("/erp/purc/poline/")
public class PurchaseOrderLineController {

    @Autowired
    private PurchaseOrderLineService purchaseOrderLineService;

    @GetMapping("list")
    @ApiOperation(value = "采购订单行列表", notes = "获取采购订单行列表（无分页）", httpMethod = "GET")
    public Result<List<PurchaseOrderLine>> list(@ApiParam(name="查询参数") String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<PurchaseOrderLine> purchaseOrderLines = purchaseOrderLineService.list(PurchaseOrderLine.class, c);
        return Result.OK(purchaseOrderLines);
    }

    @GetMapping("page")
    @ApiOperation(value = "采购订单行列表", notes = "获取采购订单行列表（分页）", httpMethod = "GET")
    public Result<Page<PurchaseOrderLine>> page(@ApiParam(name="查询参数") String criteria, @ApiParam(name="每页条数")Integer pageSize, @ApiParam(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<PurchaseOrderLine> purchaseOrderLines = purchaseOrderLineService.listPage(PurchaseOrderLine.class, page, c);
        return Result.OK(purchaseOrderLines);
    }

    @GetMapping("list/{bizNo}")
    @ApiOperation(value = "根据bizNo获取采购订单行", notes = "根据bizNo获取采购订单行", httpMethod = "GET")
    public Result<List<PurchaseOrderLine>> getByBizNo(@ApiParam(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<PurchaseOrderLine> purchaseOrderLines = purchaseOrderLineService.list(PurchaseOrderLine.class, c);
        return Result.OK(purchaseOrderLines);
    }


    @GetMapping("list/{bizNo}/{lineNo}")
    @ApiOperation(value = "根据ID获取采购订单行", notes = "根据ID获取采购订单行", httpMethod = "GET")
    public Result<PurchaseOrderLine> getById(@ApiParam(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo, @ApiParam(name="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        PurchaseOrderLine purchaseOrderLine = purchaseOrderLineService.getById(PurchaseOrderLine.class, lineKey);
        return Result.OK(purchaseOrderLine);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增采购订单行", notes = "新增采购订单行", httpMethod = "POST")
    public Result<PurchaseOrderLine> add(@RequestBody PurchaseOrderLine purchaseOrderLine) {
        purchaseOrderLineService.save(purchaseOrderLine);
        return Result.OK(purchaseOrderLine);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑采购订单行", notes = "编辑采购订单行", httpMethod = "PUT")
    public Result<PurchaseOrderLine> edit(@RequestBody PurchaseOrderLine purchaseOrderLine) {
        purchaseOrderLineService.update(purchaseOrderLine);
        return Result.OK(purchaseOrderLine);
    }

    @DeleteMapping("remove/{ids}")
    @ApiOperation(value = "根据id删除采购订单行", notes = "删除采购订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", httpMethod = "DELETE")
    public Result<PurchaseOrderLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            purchaseOrderLineService.remove(PurchaseOrderLine.class, lineKey);
        }
        return Result.OK();
    }
}
